package com.ws.controller;

import com.github.pagehelper.PageInfo;
import com.ws.domain.Goods;
import com.ws.domain.Type;
import com.ws.domain.User;
import com.ws.service.*;
import com.ws.util.PageUtil;
import com.ws.util.SafeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Types;
import java.util.*;

@Controller
@RequestMapping("/index")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CartsService cartsService;
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private TypesService typesService;


    //登录
    @RequestMapping("login")
    public String login(User user, HttpSession session, HttpServletRequest request) {
        User login = userService.login(user.getUsername(), user.getPassword());
        System.out.println(login);
        if (Objects.isNull(login)) {
            session.setAttribute("msg", "用户名或密码错误");
            return "login.jsp";
        }
        session.setAttribute("user", login);
        // 保存购物车商品的数量总数（不是种类数）
        session.setAttribute("cartCount", cartsService.getCount(login.getId()));

        // 来源页面
        String referer = request.getHeader("referer");
        System.out.println(referer);

        //重定向到首页。也可以选择重定向到来源页面。
        return "redirect:index.jsp";
    }

    //跳转到登录页面
    @RequestMapping("log")
    public String log() {
        return "login.jsp";
    }

    //跳转到注册页面
    @RequestMapping("register")
    public String register() {
        return "register.jsp";
    }

    //实现注册
    @RequestMapping("reg")
    public String reg(User user) {
        userService.userSave(user);
        //注册成功跳转到登录页面
        return "login.jsp";
    }

    //退出登录
    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "login.jsp";
    }

    //跳转到修改密码页面
    @RequestMapping("password")
    public String password() {
        return "password.jsp";
    }

    //实现修改密码
    @RequestMapping("passwordUpdate")
    public String passwordUpdate(String password, String passwordNew, HttpSession session) {
        //获取用户信息
        User user = (User) session.getAttribute("user");
        //旧密码正确
        if (passwordNew != null && user.getPassword().equals(SafeUtil.encode(password))) {
            //将新密码设置到user对象中
            user.setPassword(SafeUtil.encode(passwordNew));
            //将新密码保存到数据库中
            userService.passwordUpdate(user);
            //重新设置session
            session.setAttribute("user", user);
            //给出提示
            session.setAttribute("msg", "密码修改成功!");
        } else {
            session.setAttribute("msg", "旧密码不正确，请重新呼入!");
        }
        //响应
        return "password.jsp";
    }

    //我的购物车
    @GetMapping("/cart")
    public String cart(HttpServletRequest request, HttpSession session) {

        User user = (User) session.getAttribute("user");
        //获取已选商品
        request.setAttribute("cartList", cartsService.getList(user.getId()));
        //获取已选商品总数量(不是总类数)
        request.setAttribute("cartCount", cartsService.getCount(user.getId()));
        //获取已选商品总价格
        request.setAttribute("cartTotal", cartsService.getTotal(user.getId()));

        return "/index/cart.jsp";
    }


    //首页显示数据
    @RequestMapping("/indenx.to")
    public String index(HttpSession session) {
        //1.高亮显示"商城首页"。
        session.setAttribute("flag", 1);
        //2、查询今日推荐前六个
        List<Goods> todayList = goodsService.Sixfirst(0, 6); // 取前6个
        session.setAttribute("todayList", todayList);
        //3、销量前8个商品：根据销量降序选择limit 0，8
        List<Goods> hotList = goodsService.XiaoEight(0, 8);//只显示前面8个
        session.setAttribute("hotList", hotList);
        //4、15个生态果蔬、肉禽蛋奶、米面油粮、休闲零食、玩具、户外用品
        //商品类型集合。
        List<Type> typeList = typesService.getList();
        session.setAttribute("typeList", typeList);
        //把每一种商品类型的该类型前15个商品封装到一个map中，形成一个List<map>。 最后遍历展示。
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (Type type : typeList) {
            Map<String, Object> map = new HashMap<>();
            map.put("type", type);
            map.put("goodList", goodsService.Swu(type.getId(), 1, 15));
            dataList.add(map);
        }
        session.setAttribute("dataList", dataList);
        //5、跳转到首页显示数据
        return "index.jsp";

    }


    //更多推荐
    @RequestMapping("/today")
    public String today(HttpServletRequest request,
                        @RequestParam(required = false, defaultValue = "1") int page,
                        @RequestParam(required = false, defaultValue = "5") int size) {

        //1、高亮选中今日推荐
        request.setAttribute("flag", 2);
        //2、分页查询某一个今日推荐商品,Sixfirst:调用是选中今日推荐前六个商品同样的方法
        request.setAttribute("goodList", goodsService.Sixfirst(page, size));
        //3、设置分页信息

//        System.out.println(goodsService.Sixfirst( page, size));
        return "goods.jsp";
    }

    //更多热销
    @RequestMapping("/hot")
    public String hot(HttpServletRequest request,
                      @RequestParam(required = false, defaultValue = "1") int page,
                      @RequestParam(required = false, defaultValue = "5") int size) {

        //1、高亮选中热销排行
        request.setAttribute("flag", 3);

        //2、分页查询热销商品
        request.setAttribute("goodList", goodsService.XiaoPai());
//        System.out.println(goodsService.XiaoPai());
        //3、设置分页信息：查询所有商品数量，热销商品是根据商品的销量降序查询


        return "goods.jsp";
    }


    //查看更多
    @RequestMapping("/type")
    public String type(HttpServletRequest request,
                       int id,
                       @RequestParam(required = false, defaultValue = "1") int page,
                       @RequestParam(required = false, defaultValue = "5") int size) {

        //2、分页查询指定类型商品
        request.setAttribute("goodList", goodsService.Swu(id, page, size));
        List<Goods> list=goodsService.Swu(id, page, size);

        //3、设置分页信息
        PageInfo<Goods> pageInfo  = new PageInfo<>(list);

        request.setAttribute("pageInfo",pageInfo);

        return "goods.jsp";
    }

}
